package com.xn.app.muying.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.xn.app.muying.R;
import com.xn.app.muying.activity.ChanJianDetailActivity;
import com.xn.app.muying.activity.ChanJianDetailItemActivity;
import com.xn.app.muying.adpter.ChanJianDetailAdapter;
import com.xn.app.muying.bean.ChanJianMessageBean;
import com.xn.app.muying.view.MyGridView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 产检详情的页面显示
 * Created by gaoge on 2017/9/12.
 */
@SuppressLint("ValidFragment")
public class ChanJianFragment extends Fragment {

    //得到的数据
    private String content = null;
    //显示孕周
    private TextView week = null;
    //显示产检重点
    private TextView keypoint = null;
    //显示必查项目
    private MyGridView required = null;
    //显示备查项目
    private MyGridView optional = null;
    //gridview 适配器
    ChanJianDetailAdapter chanJianItemsAdapter = null;
    //javabean
    ChanJianMessageBean chanJianMessageBean =null;


    public static ChanJianFragment newInstance(String content) { // 对外提供创建实例的方法，你给我需要显示的内容，我给你Fragment实例
        return new ChanJianFragment(content);
    }

    public ChanJianFragment(String content) {
        this.content = content;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        //加载布局
        View view = inflater.inflate(R.layout.fragment, container, false);
        //初始化组件
        week = (TextView) view.findViewById(R.id.week);
        keypoint = (TextView) view.findViewById(R.id.keypoint);
        required = (MyGridView) view.findViewById(R.id.required);
        optional = (MyGridView) view.findViewById(R.id.optional);
        //功能实现
        function();
        return view;
    }

    /**
     * 功能实现
     */
    private void function() {
        try {
            //保存必查项目数据
            final ArrayList<ChanJianMessageBean> requiredList = new ArrayList<>();
            //保存备查项目数据
            final ArrayList<ChanJianMessageBean> optionalList = new ArrayList<>();
            //获取数据
            JSONObject jsonObject = new JSONObject(content);
            String startWeek = jsonObject.optString("startWeek");
            String endWeek = jsonObject.optString("endWeek");
            String mkeypoint = jsonObject.optString("keyPoint");
            //获取必查项目
            JSONArray requiredItems = jsonObject.getJSONArray("requiredItems");
            JSONObject jsonObject1;
            for (int a = 0; a < requiredItems.length(); a++) {
                jsonObject1 = requiredItems.getJSONObject(a);
                chanJianMessageBean = new ChanJianMessageBean();
                chanJianMessageBean.setName(jsonObject1.getString("name"));
                chanJianMessageBean.setExplain(jsonObject1.getString("explain"));
                chanJianMessageBean.setTip(jsonObject1.getString("tip"));
                chanJianMessageBean.setId(jsonObject1.getString("id"));
                requiredList.add(chanJianMessageBean);
            }
            //获取备查项目
            JSONArray optionalItems = jsonObject.getJSONArray("optionalItems");
            for (int a = 0; a < optionalItems.length(); a++) {
                jsonObject1 = optionalItems.getJSONObject(a);
                chanJianMessageBean = new ChanJianMessageBean();
                chanJianMessageBean.setName(jsonObject1.getString("name"));
                chanJianMessageBean.setExplain(jsonObject1.getString("explain"));
                chanJianMessageBean.setTip(jsonObject1.getString("tip"));
                chanJianMessageBean.setId(jsonObject1.getString("id"));
                optionalList.add(chanJianMessageBean);
            }
            //显示孕周
            week.setText(startWeek + "周-" + endWeek + "周");
            //显示产检重点
            keypoint.setText(mkeypoint);
            //设置必查项目
            if (requiredList.size() > 0) {
                chanJianItemsAdapter = new ChanJianDetailAdapter(getActivity(), requiredList);
                required.setAdapter(chanJianItemsAdapter);
                //添加点击事件
                required.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent=new Intent(getActivity(), ChanJianDetailItemActivity.class);
                        intent.putExtra("name",requiredList.get(position).getName());
                        intent.putExtra("tip",requiredList.get(position).getTip());
                        intent.putExtra("explain",requiredList.get(position).getExplain());
                        startActivity(intent);
                    }
                });
            }
            //设置备查项目
            if (optionalList.size() > 0) {
                chanJianItemsAdapter = new ChanJianDetailAdapter(getActivity(), optionalList);
                optional.setAdapter(chanJianItemsAdapter);
                //添加点击事件
                optional.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent=new Intent(getActivity(), ChanJianDetailItemActivity.class);
                        intent.putExtra("name",optionalList.get(position).getName());
                        intent.putExtra("tip",optionalList.get(position).getTip());
                        intent.putExtra("explain",optionalList.get(position).getExplain());
                        startActivity(intent);
                    }
                });
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
